package com.chongzi.cloud;

public class code5 {
    public static void main(String[] args) {
        String s="babad";
        String s1 = longestPalindrome(s);
        System.out.println(s1);
    }
    public  static  String longestPalindrome(String s) {
        int res_len = 0;
        int res_start = 0;
        char[] array = s.toCharArray();
        for (int i = 0; i < array.length; i++) {
            int L = i, R = i;
            while (L >= 0 && R <= s.length()-1 && array[L] == array[R]) {
                if (R - L + 1 > res_len) {
                    res_len = R - L + 1;
                    res_start = L;
                }
                L--;
                R++;
            }

            L = i;
            R = i + 1;
            while (L >= 0 && R <= s.length()-1 && array[L] == array[R]) {
                if (R - L + 1 > res_len) {
                    res_len = R - L + 1;
                    res_start = L;
                }
                L--;
                R++;
            }

        }
        return s.substring(res_start, res_len);
    }
}
